layui.use(['table', 'admin', 'ax', 'ztree', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var $ZTree = layui.ztree;
    var func = layui.func;

    /**
     * 数据字典表管理
     */
    var Dict = {
        tableId: "dictTable",
        condition: {
            id: ""
        }
    };

    /**
     * 初始化表格的列
     */
    Dict.initColumn = function () {
        return [[

            {field: 'id', hide: true, title: '主键'},
            {field: 'name', sort: true, title: '字典名称 : 字典名称', width: 320},
            {field: 'dictDescribe', sort: true, title: '描述', minWidth: 320},
            {field: 'dictValue', sort: true, title: '值', width: 160},
            {
                field: 'image', sort: true, title: '图片', templet: function (d) {
                    if (d.images != '') {
                        return '<div onclick=showMedia("img","image","' + d.images + '") >' +
                            '<img id="img" src="' + d.images + '" alt="" width="100px" height="50px"></ a></div>';
                    } else {
                        return '';
                    }
                }
            },
            {field: 'createTime', sort: true, title: '创建时间', width: 180},
            {field: 'updateTime', sort: true, title: '更新时间', width: 180},
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 120}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Dict.search = function () {
        var queryData = {};
        queryData['parentId'] = Dict.condition.id;
        queryData['name'] = $("#condition").val();
        table.reload(Dict.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 选择分类时
     */
    Dict.onClickTDict = function (e, treeId, treeNode) {
        Dict.condition.id = treeNode.id;
        Dict.search();
    };


    /**
     * 弹出添加对话框
     */
    Dict.openAddDlg = function () {
        if (Dict.condition.id === '') {
            Feng.error("请选择上级字典后添加子项！");
        } else {
            if (Dict.condition.id == null) {
                Dict.condition.id = 0;
            }

            func.open({
                title: '添加数据字典',
                content: Feng.ctxPath + '/tDict/add?parentId=' + Dict.condition.id,
                tableId: Dict.tableId
            });
        }

    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Dict.openEditDlg = function (data) {
        func.open({
            title: '修改数据字典表',
            content: Feng.ctxPath + '/tDict/edit?id=' + data.id,
            tableId: Dict.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    Dict.exportExcel = function () {
        var checkRows = table.checkStatus(Dict.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Dict.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/tDict/delete", function (data) {
                if (data.code == 200) {
                    Feng.success("删除成功!");
                } else {
                    Feng.error("删除失败!" + data.message + "!");
                }
                table.reload(Dict.tableId);
                ztree.init();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Dict.tableId,
        url: Feng.ctxPath + '/tDict/findPage',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Dict.initColumn()
    });

    //初始化左侧部门树
    var ztree = new $ZTree("tDictTree", "/tDict/tree");
    ztree.bindOnClick(Dict.onClickTDict);
    ztree.init();


    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Dict.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Dict.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Dict.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Dict.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Dict.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Dict.onDeleteItem(data);
        }
    });
});

function showMedia(id, type, src) {
    var $ = layui.jquery;
    var idBar = "#" + id;
    var width = $(idBar).width();
    var height = $(idBar).height();
    var scaleWH = width / height;
    var bigH = 500;
    var bigW = scaleWH * bigH;
    if (bigW > 900) {
        bigW = 900;
        bigH = bigW / scaleWH;
    } // 放大预览图片
    layer.open({
        type: 1,
        title: false,
        closeBtn: 1,
        shadeClose: true,
        area: [bigW + 'px', bigH + 'px'], //宽高
        content: "<img width='" + bigW + "' height='" + bigH + "'controls=\"controls\" src=" + src + " />"
    });
}